<template>
  <div>
    <el-form ref="param" :model="param" :rules="rules"  label-width="120px">
      <el-form-item label="老师姓名:" prop="teacherName">
        <el-input v-model="param.teacherName" size="mini" clearable style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="归属学校:" prop="schoolId">
        <el-select v-model="param.schoolId" placeholder="请选择" size="mini" style="width:80%" clearable @change="selSch($event)">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话号码:" prop="phonenumber">
        <el-input v-model="param.phonenumber" size="mini" clearable style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="简介:" prop="teacherIntro">
        <el-input v-model="param.teacherIntro" type="textarea" size="mini" clearable style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="详细介绍:" prop="detailIntro">
        <el-input v-model="param.detailIntro" type="textarea" size="mini" clearable style="width:80%" rows="4"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="editor()">修改</el-button>
      <el-button @click="handCancle" size="mini">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getList } from '@/api/school/school'
import { getEdi, update, } from '@/api/teacher/teacher'
export default {
  props: ['id', 'showDialog'],
  data() {
    return {
      form: {
        name: ''
      },
      value: '',
      options: [],
      param: {},
      rules: {
        teacherName: [
          { required: true, message: '请输入老师名称', trigger: 'blur' }
        ],
        teacherIntro: [
          { required: true, message: '请输入老师简介', trigger: 'blur' }
        ],
        detailIntro: [
          { required: true, message: '请输入详细介绍', trigger: 'blur' }
        ],
        schoolId: [
          { required: true, message: '请选择所属学校', trigger: 'blur' }
        ],
        phonenumber: [
           { required: true, message: '请输入手机号', trigger: 'blur' },
           { pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEdi()
    this.getList()
  },
  methods: {
    selSch(value) {
      let obj = {}
      obj = this.options.find((item) => {
        return item.value == value
      })
      this.param.schoolName = obj.label
    },
    getList() {
      getList().then(res => {
        let arr = res.rows
        let arr1 = []
        for(let i=0;i<arr.length;i++){
          arr1.push({'value': arr[i].id, 'label': arr[i].schoolName})
        }
        this.options = arr1
      })
    },
    editor() {
      this.$refs['param'].validate((valid) => {
        if (valid) {
          update(this.param).then(res => {
            if(res.code === 200){
              this.$message.success('修改成功')
              this.$emit('handCancle', 1)
            }
          })
        } else {
          return false;
        }
      })
    },
    getEdi() {  
      getEdi(this.id).then(res => {
        this.param = res.data
      })
    },
    handCancle() {
      this.$emit('handCancle')
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  },
  watch: {
      showDialog: function(n, o){
        if(n) {
          this.param.id = this.id
          this.getEdi()
        }
      }
  }
}
</script>

<style scoped>
.dialog-footer{
  text-align: center;
}
.img{
  width: 40px;
  height: 40px;
}
.blod {
  font-weight: bold;
}
.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-table th.gutter {
  display: table-cell !important
}
</style>
